package togoogle;
import java.io.IOException;
import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;

import javax.jdo.PersistenceManager;
import java.util.Map;
import java.util.logging.Logger;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.xml.sax.InputSource;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.sample.structuredcontent.basicexample.StructuredContentSample;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.domain.Item;
import com.taobao.api.request.ItemsListGetRequest;
import com.taobao.api.response.ItemsListGetResponse;


import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
import togoogle.common.Constant;
import togoogle.common.Util;

@Controller
public class geturlcontroller {
	private static final Logger log = Logger.getLogger("Main"); 

        @RequestMapping(value = "/togoogle/geturl",method = RequestMethod.GET)
        public String doGet(String page,String productid,Model model) throws IOException {
        	String fields="detail_url,pic_url";
        	ItemsListGetRequest request=new ItemsListGetRequest();
        	ItemsListGetResponse respons=null;
        	request.setFields(fields);
        	request.setNumIids(productid);
        	request.setTimestamp((new Date()).getTime());
        	DefaultTaobaoClient cli=new DefaultTaobaoClient(Constant.SANDBOX_URL, Constant.APP_KEY, Constant.APP_SERCET);
        	try {
        		respons=cli.execute(request);
        		if(respons==null || !respons.isSuccess()){
        			log.info(respons.getBody());
        			return "error";
        		}
        	} catch (ApiException e) {
        		// TODO Auto-generated catch block
        		log.info(e.getErrMsg());
        		return "error";
        	}
        	List<Item> listitems=respons.getItems();
        	Item it=listitems.get(0);
        	log.info(it.getDetailUrl());
        	log.info(it.getPicUrl());
        	model.addAttribute("detail_url", it.getDetailUrl());
        	model.addAttribute("img_url", it.getPicUrl());
        	if(page.equals("detail")){
        		return "productdetail";
        	}else if(page.equals("img")){
        		return "img";
        	}
        	return "error";
        }
}